package dao.Impl;

import dao.AccountDao;
import entity.Account;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author wfr
 * @description:
 * @date 2019-05-31 15:14
 * @parem AccountDaoImpl
 */
public class AccountDaoImpl extends BaseDao  implements AccountDao {
    public AccountDaoImpl() {super();
    }

    @Override
    public int add(Account account) throws SQLException {
        int r=0;
        try {
            PreparedStatement pst =con.prepareStatement("insert into account(DEAL_TYPE,PET_ID,SELLER_ID,BUYER_ID,PRICE,DEAL_TIME) value (?,?,?,?,?,?)");
            pst.setInt(1,account.getDealType());
            pst.setInt(2,account.getPetId());
            pst.setInt(3,account.getSellerId());
            pst.setInt(4,account.getBuyerId());
            pst.setInt(5,account.getPrice());
            pst.setDate(6,account.getDealTime());

            r=pst.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }

        return r;
    }

    @Override
    public Account getBtId(int id) {
        Account account = null;
        try {

            ResultSet resultSet;
            resultSet = con.createStatement().executeQuery("select * from account where ID=" + id);
            if (resultSet.next()) {
                account = new Account();
                account.setId(resultSet.getInt(1));
                account.setDealType(resultSet.getInt(2));
                account.setPetId(resultSet.getInt(3));
                account.setSellerId(resultSet.getInt(4));
                account.setBuyerId(resultSet.getInt(5));
                account.setPrice(resultSet.getInt(6));
                account.setDealTime(resultSet.getDate(7));

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return account;
    }

    @Override
    public List<Account> getlist(int sellid,int dealtype) {
        List<Account> accounts = new ArrayList<Account>();
        try {
            ResultSet resultSet;
            String sql = "select * from account where SELLER_ID=? and DEAL_TYPE=?";
            PreparedStatement pst;
            pst = con.prepareStatement(sql);
            pst.setInt(1, sellid);
            pst.setInt(2, dealtype);
            resultSet = pst.executeQuery();
            Account account = null;
            // 设置条件(相对于条件查询)

            while (resultSet.next()) {
                account = new Account();
                account.setId(resultSet.getInt(1));
                account.setDealType(resultSet.getInt(2));
                account.setPetId(resultSet.getInt(3));
                account.setSellerId(resultSet.getInt(4));
                account.setBuyerId(resultSet.getInt(5));
                account.setPrice(resultSet.getInt(6));
                account.setDealTime(resultSet.getDate(7));
                accounts.add(account);
            }

        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return accounts;
    }
}
